<template>
  <div style="height: 100%">
    <div>
      <LeftHead @tellFather="changeShowData"></LeftHead>
    </div>
    <div style="width: 100%">
      <div v-if="showForums.length == 0">
        <a-skeleton avatar :paragraph="{ rows: 4 }" />
        <a-skeleton avatar :paragraph="{ rows: 4 }" />
        <a-skeleton avatar :paragraph="{ rows: 4 }" />
      </div>
      <div v-else>
        <ForumItem
          v-if="selection == 0"
          v-for="(item, index) in focusForums"
          :key="index"
          :forum="item"
        ></ForumItem>
        <ForumItem
          v-if="selection == 1"
          v-for="(item, index) in goodForums"
          :key="index"
          :forum="item"
        ></ForumItem>
        <ForumItem
          v-if="selection == 2"
          v-for="(item, index) in myForums"
          :key="index"
          :forum="item"
        ></ForumItem>
      </div>
    </div>
  </div>
</template>

<script>
import ForumItem from "./ForumItem";
import LeftHead from "./LeftHead";
export default {
  name: "ForumLeft",
  components: {
    ForumItem,
    LeftHead,
  },
  data() {
    return {
      isWaiting: true,
      uid: this.$store.state.id,
      focus: [],
      selection: 0,
      allForums: [],
      myForums: [],
      showForums: [],
      focusForums: [],
      goodForums: [],
    };
  },

  methods: {
    updateData() {
      console.log("刷新");
      this.allForums = [];
      this.myForums = [];
      this.showForums = [];
      this.focusForums = [];
      this.goodForums = [];
      this.getForum();
    },
    async getFocus(uid) {
      try {
        let res = await this.$axios.get("/focus/selectByUid?uid=" + uid);
        let data = res.data.data;
        this.focus = data;
        console.log("关注", this.focus);
      } catch (error) {
        console.log(error);
      }
    },
    async getForum() {
      try {
        await this.getFocus(this.uid);
        let t_res = await this.$axios.get(
          "/dynamic/getAllPaging?pageNo=1&pageSize=2"
        );
        let total = t_res.data.data.total;

        // 关注的动态
        let f_res = await this.$axios.get(
          "dynamic/selectFocusByUidPaging?uid=" +
            this.uid +
            "&pageNo=1&pageSize=" +
            total
        );
        let f_data = f_res.data.data;
        // console.log("关注的动态---", f_data);
        for (let i = 0; i < f_data.length; i++) {
          let forum = await this.transform(f_data[i]);
          // console.log("处理完的动态---", forum);
          forum.isFocus = true;
          this.focusForums.push(forum);
        }
        this.changeShowData(this.selection);

        let res = await this.$axios.get(
          "/dynamic/getAllPaging?pageNo=1&pageSize=" + total
        );
        let data = res.data.data.records;
        console.log("动态---", data);
        // console.log("用户id-----", f_data);
        for (let i = 0; i < data.length; i++) {
          let forum = await this.transform(data[i]);
          // console.log("处理完的动态---", forum);
          for (let l = 0; l < this.focus.length; l++) {
            if (forum.uid == this.focus[l].id) {
              forum.isFocus = true;
            }
          }
          // 我的动态
          if (forum.uid == this.uid) {
            forum.isFocus = true;
            this.myForums.push(forum);
          }
          // 所有动态
          this.allForums.push(forum);
        }
        this.changeShowData(this.selection);
        // 按点赞数排序动态
        let arr = this.allForums;
        var len = arr.length;
        for (var i = 0; i < len - 1; i++) {
          for (var j = 0; j < len - 1 - i; j++) {
            if (arr[j].love[1] < arr[j + 1].love[1]) {
              // 相邻元素两两对比
              var temp = arr[j + 1]; // 元素交换
              arr[j + 1] = arr[j];
              arr[j] = temp;
            }
          }
        }
        this.goodForums = arr;
        // 默认展示精选
        // this.showForums = this.goodForums;
        this.changeShowData(this.selection);
      } catch (error) {
        // console.log("获取动态失败---", error);
      }
    },
    async getUserById(uid) {
      try {
        // // console.log(uid);
        let res = await this.$axios.get("/user/selectOneById?id=" + uid);
        let data = res.data.data;
        // console.log("user---", data);
        return data;
      } catch (error) {
        // console.log("获取用户失败---", error);
      }
    },
    async getCommentByDid(did) {
      try {
        // console.log("动态id", did);
        let res = await this.$axios.get("/comment/selectByDid?did=" + did);
        let data = res.data.data;
        // console.log("comments---", data);
        return data;
      } catch (error) {
        // console.log("获取评论失败---", error);
      }
    },
    async transform(data) {
      let forum = {
        authorDetail: {},
        uid: "",
        did: "",
        headImg: "",
        name: "",
        time: new Date(),
        contentText: "",
        isFocus: false,
        contentImg: [],
        love: [0, 12], // 是否点赞，点赞人数
        comment: 23, // 评论人数
        // collect: [1, 2], // 是否收藏，收藏人数
        comments: [],
      };
      forum.uid = data.uid;
      forum.did = data.id;
      forum.contentText = data.dynamicContent;
      forum.time = new Date(data.createTime);
      forum.love[1] = data.loveNumber;

      for (let j = 1; j < data.imgCount + 1; j++) {
        forum.contentImg.push(
          "http://47.108.176.163:7777/img_dynamic/" + data.id + "_" + j + ".png"
        );
      }
      let t_user = await this.getUserById(data.uid);
      forum.name = t_user.userName;
      let authorDetail = {
        briefIntroduction: t_user.briefIntroduction,
        authorId: t_user.id,
        occupation: t_user.occupation,
        sex: t_user.sex,
        userName: t_user.userName,
        headImg:
          "http://47.108.176.163:7777/img_user_head/" + t_user.id + ".png",
      };
      forum.authorDetail = authorDetail;
      // console.log("作者---", forum.authorDetail);
      forum.headImg =
        "http://47.108.176.163:7777/img_user_head/" + data.uid + ".png";
      let t_comment = await this.getCommentByDid(data.id);
      // // console.log("评论啊", t_comment);
      forum.comment = t_comment.length;
      for (let k = 0; k < t_comment.length; k++) {
        let comment = {
          headImg: "",
          name1: "",
          name2: "",
          time: new Date(),
          content: "",
        };
        comment.headImg =
          "http://47.108.176.163:7777/img_user_head/" +
          t_comment[k].uid +
          ".png";
        comment.name1 = t_comment[k].userName;
        comment.time = new Date(t_comment[k].createTime);
        comment.content = t_comment[k].commentContent;
        forum.comments.push(comment);
      }
      return forum;
    },
    changeShowData(value) {
      this.selection = value;
      if (value == "0") {
        this.showForums = this.focusForums;
      } else if (value == "1") {
        this.showForums = this.goodForums;
      } else if (value == "2") {
        this.showForums = this.myForums;
      }
    },
    async haveAdd(newForum) {
      let forum = await this.transform(newForum);
      console.log("传到左边的新动态---", forum);
      // if (this.selection != 0) {
      //   this.showForums.push(forum);
      // }
      this.myForums.unshift(forum);
      this.goodForums.unshift(forum);
      this.allForums.unshift(forum);
    },
  },
  mounted() {
    if (this.showForums.length <= 0) {
      this.getForum();
    }
    // this.$refs.LeftHaed.setValue(this.selection);
  },
};
</script>
